package servlet;

import bean.News;
import bean.NewsofUser;
import bean.User;
import dao.NewsDao;
import dao.UserDao;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public LoginServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = UserDao.login(username, password);
        if (user != null) {
                if(0==user.getNote()){
                    request.getSession().setAttribute("isUserAdmin", true);
                }else {
                    request.getSession().setAttribute("isUserAdmin", false);
                }
                request.getSession().setAttribute("userName", username);
                request.getSession().setAttribute("user", user);
               // request.getSession().setAttribute("user", user);
              //  response.session.setAttribute(“username”,username)
                List<NewsofUser> list =  NewsDao.select();
                //request.setAttribute("list", list);
                request.getSession().setAttribute("list", list);

                List<User> userlist =  UserDao.select();

                //request.setAttribute("list", list);
                request.getSession().setAttribute("userlist", userlist);

                response.sendRedirect("./jsp/main.jsp"); // Redirect to welcome page
//                response.sendRedirect("/press_release_system_war_exploded/testlimit"); // Redirect to welcome page
            } else {
                System.out.println("用户名或密码错误");

                request.getSession().setAttribute("from", "loginServlet");
                request.getSession().setAttribute("errorMessage", "用户名或密码错误");
                response.sendRedirect("./jsp/login.jsp");
            }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
